package com.balance.anole.utils;

/**
 * @author zhouxiaobo
 *
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

public class DateUtil {
	private static String timezone = "Asia/Singapore";
	private static TimeZone tz = TimeZone.getTimeZone(timezone);
	public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS="yyyyMMddHHmmss";
	public static final String DEFALT_FORMAT_DATETIME = "dd/MM/yyyy HH:mm:ss";
	public static final String DEFALT_FORMAT_DATETIME2 = "yyyy-MM-dd HH:mm:ss";
	public static final String DEFALT_FORMAT_DATE = "dd/MM/yyyy";
	public static final String DEFALT_FORMAT_TIME = "HH:mm:ss";
	public static final String DEFALT_FORMAT_REPORT_DATE = "yyyy/MM/dd";
	public static final String DEFALT_FORMAT_INPUT_REPORT_DATE = "yyyyMMdd";
	public final static String DATE_FORMAT_YYYY_MM_DD="yyyy-MM-dd";
	private static Map<String, SimpleDateFormat> formatPool = new HashMap<String, SimpleDateFormat>();


    public static Calendar getCalendar() {
    	return Calendar.getInstance(tz);
    }

    /**
     * Return the Date instance with the provided year,
     * month ( 1 ? 12 ), and day ( 1 - 31 ) values.
     * <p>
     * The date value will roll over when given a value that is greater
     * than the max possible. Eg. when getDate( 2002, 10, 32 )
     * is provided, the Date instance will be 1st Nov 2002.
     * <p>
     * @param  year  Year
     * @param  month Month ( 1 - 12 )
     * @param  day   Day( 1 - 31 )
     * @return The Date instance created.
     */
    public static Date getDate (int year, int month, int day) {
        Calendar cal = getCalendar();
        // Clear all fields first
        cal.clear();
        cal.set( year, month - 1, day );

        return cal.getTime();
    }

    /**
     * Returns the date instance based on the current system date
     * <p>
     * @return Current System date.
     */
    public static Date getSystemCurrentDate() {
        return getCalendar().getTime();
    }

    public static Date getTranCurrentDate() {
    	//TODO: Maybe it's need be define in the DB.
    	return getCalendar().getTime();
    }

	/**
	 * @param date
	 * @return
	 */
    public static Date getDateWithOutTime(Date date){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFALT_FORMAT_DATE);
		String dateWithoutTime = simpleDateFormat.format(date);
		try {
			return simpleDateFormat.parse(dateWithoutTime);
		} catch (ParseException e) {
			return null;
		}
	}


	public static java.util.Date stringToDate(String inputStr, String format) {
		if (inputStr == null || inputStr.trim().equals("") || format == null
				|| "".equals(format)) {
			return null;
		}

		Date result = null;
		try {
			result = getDateFormat(format).parse(inputStr);
		} catch (Exception e) {

		}
		return result;
	}

	/**
	 * get data format form formatter pool.
	 *
	 * @param formatType
	 * @return SimpleDateFormat.
	 */
	public static SimpleDateFormat getDateFormat(String formatType) {
		SimpleDateFormat sdf = formatPool.get(formatType);
		if (sdf == null) {
			sdf = new SimpleDateFormat(formatType);
			formatPool.put(formatType, sdf);
		}
		return sdf;
	}

	/**
	 * Date format to String
	 * @param date
	 * @param format
	 * @return
	 */
	public static String format(Date date,String format){
		SimpleDateFormat sf=new SimpleDateFormat(format);
		return sf.format(date);
	}

	public static String getSysCurrentDate(String format){
		return format(getSystemCurrentDate(), format);
	}

	/**
	 * Get next date
	 * @param date
	 * @return
	 */
	public static Date nextDate(Date date){
		 Calendar cal = getCalendar();
		 cal.add(Calendar.DATE, 1);
		return cal.getTime();
	}

}
